<template>
    <h2>添加权益</h2>
    <table border="1">
        <tr>
            <td>权益名称</td>
            <td>
                <input type="text" v-model="state.rightsName">
            </td>
        </tr>
        <tr>
            <td>创建时间</td>
            <td><input type="date" v-model="state.createDate"></td>
        </tr>
        <tr>
            <td>修改时间</td>
            <td><input type="date" v-model="state.updDate"></td>
        </tr>
        <tr>
            <td></td>
            <td>
                <input type="button" value="增加" @click="addright">
            </td>
        </tr>
    </table>
</template>

<script setup lang="ts">
import {reactive,toRefs} from 'vue';
import axios from 'axios';
import {useRouter} from 'vue-router';
const router = useRouter()

const data = reactive({
    state:{
    "rightsName": "",
  "createDate": "",
  "updDate": ""
    }
    
})

const {state} = toRefs(data)

const addright=()=>{
    if(state.value.rightsName=='')
    {
        alert('权益名称不能为空');
            return;
    }
    let reg= /^[\u4e00-\u9fa5]{1,9}$/;
    if(!reg.test(state.value.rightsName))
    {
        alert('权益名称只能是1-9个汉字');
            return;
    }
    if(state.value.createDate=='')
    {
        alert('创建时间不能为空');
            return;
    }
    if(state.value.updDate=='')
    {
        alert('修改时间不能为空');
            return;
    }
    axios({
        url:'/api/Member/AddRights',
        method:'post',
        data:state.value
    })
    .then(res=>{
        console.log(res)
        if(res.data.data>0)
        {
            alert('增加成功');
            //router.push('/')
        }
        else if(res.data.data==0){
            alert('权益名称重复');
            return;
        }
        else{
            alert('增加失败');
            return;
        }
    })
    .catch(err=>{
        console.log(err)
    })
}


</script>